<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            托物料转移
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">新托码</div>
                    <input type="text" class="aui-input" placeholder="请扫描新托码" style="width: 80%;" id="trayyinfoidnew"
                        onclick="getIdSelector();" onfocus="focusFn(this)">
                    <!-- <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner(1);"></i> -->
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner" style="width: 45%;">
                    <div class="aui-list-item-label" style="margin-right: 1rem;">总箱数</div>
                    <input type="text" class="aui-input" style="width: 60%;" id="countQty" disabled="true">
                </div>
                <div class="aui-list-item-inner" style="width: 55%;">
                    <div class="aui-list-item-label" style="width: 50%;margin-right: 1rem;">物料数量</div>
                    <input type="text" class="aui-input" style="width: 80%;" id="sumQty" disabled="true">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 目标库位" id="workCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i id="cellSelect" class="aui-input-addon aui-iconfont aui-icon-right"
                        onclick="goSelectWorkCell('2')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱码</div>
                    <input type="text" class="aui-input" placeholder="请扫描箱码" style="width: 80%;" id="tm"
                        onclick="getIdSelector();" onfocus="focusFn(this)">
                    <!-- <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner(3);"></i> -->
                </div>
            </li>

        </ul>

        <div style="width:100%;margin-top:2%;"></div>
        <div class="aaui-btn-info aui-btn-block" id="infoDiv"
            style="margin-bottom: 10px;color: black !important;background-color: #c2c2c2 !important;height: 125px;padding: 0.2rem 0.2rem;font-size: 0.8rem;">
            提示信息：</br>
            <p id="resultInfo" style="color: #000000;font-size: 0.8rem;"></p>

        </div>

        <!-- <div style="width:100%;margin-top:2%;"></div>
        <div class="aaui-btn-info aui-btn-block" 
        style="margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 110px;padding: 0.2rem 0.2rem;" 
        >
        <table style="width:100%;">
            <tr style="width:100%;">
                <td style="width:25%;">箱号：</td><td  id='boxnumInfo' style="width:70%;" colspan="3"></td> 
            </tr>
            <tr style="width:100%;">
                <td style="width:25%;">物料号：</td> <td  id='mrlCodeInfo' style="width:70%;" colspan="3"></td> 
            </tr>
            <tr style="width:100%;">
                <td style="width:25%;">批次号：</td><td id='lotCodeInfo' style="width:30%;" ></td> <td style="width:18%;">数量：</td><td id='qtyInfo' style="width:30%;"></td>
            </tr>
        </table>
        
        </div> -->

        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:20%;" id="tbodyHtml">

        </div>


    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var scanRule = ""; //条码解析规则
    //光标所在id
    var idSelector = "";
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称
    var isSFK = false;//登录用户是否是寄售服务库方账号
    //初始化查询仓库下拉框
    apiready = function () {

        $("#trayyinfoidnew").focus();
        idSelector = "trayyinfoidnew";
        //查询条码解析规则
        initScanRule();
        //查询用户身份
        getUserInfo();
        initSearch();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }
    //初始化，判断登录用户是不是三方库的，是则默认显示三方库仓库
    function initSearch() {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getInitSHWorkCenterTray";
        var data = {
            values: {
                appVersion: api.appVersion,
                un: un,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    sfkFlag = myobj.sfkFlag;
                    initWorkCellId = myobj.workCellId;
                    initWorkCellCode = myobj.workCellCode;
                    initWorkCellName = myobj.workCellName;
                    //三方库账号
                    if (sfkFlag == "Y") {
                        workCellGid = initWorkCellId;
                        workCellCode = initWorkCellCode;
                        workCellName = initWorkCellName;
                        document.getElementById("workCellCode").value = initWorkCellCode + ":" + initWorkCellName;
                       // document.getElementById("workCellCode").value = workCellCode;
                    }


                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("连接超时，请耐心等待");
            }
        });
    }
    function getUserInfo() {

        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getUserInfo";
        var data = {
            values: {
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    // isSFK=true;
                    var list = eval(myobj.map);
                    if (list.length > 0) {
                        //是供应商用户
                        var map = list[0];
                        isSFK = map["supplierType"] == 2 ? true : false;
                    }

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            }
        });
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }

    //箱码转移
    function save() {
        var trayyinfoidnew = $("#trayyinfoidnew").val();
        // var workCellCode = $("#workCellCode").val();
        var tm = $("#tm").val();



        //托码新
        if (trayyinfoidnew == "" || trayyinfoidnew == null || trayyinfoidnew == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "请扫描托码！";
            $("#tm").val("");
            // api.toast({
            //     msg: '请扫描托码！',
            //     duration: 3000
            // });
            return;
        }

        //库位
        if (workCellCode == "" || workCellCode == null || workCellCode == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "请扫描/选择库位！";
            $("#tm").val("");
            // api.toast({
            //     msg: '请扫描/选择库位！',
            //     duration: 3000
            // });
            return;
        }

        //条码
        if (tm == "" || tm == null || tm == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "请扫描条码！";
            // api.toast({
            //     msg: '请扫描条码！',
            //     duration: 3000
            // });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmTrayinfoMrlMove";
        var data = {
            values: {
                appVersion: api.appVersion,
                trayyinfoid: trayyinfoidnew, //托码
                tm: tm, //条码
                workCellCode: workCellCode,//库位
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            api.hideProgress();
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    document.getElementById('infoDiv').style.background = "#66f10b";
                    var boxnum = myobj.boxnum;
                    var mrlCode = myobj.mrlCode;
                    var lotCode = myobj.lotCode;
                    var qty = myobj.qty;
                    var sumQty = myobj.sumQty;
                    var countQty = myobj.countQty;
                    $("#sumQty").val(sumQty);
                    $("#countQty").val(countQty);
                    document.getElementById("resultInfo").innerHTML = myobj.message;
                    $("#tm").val("");
                    $("#tm").select();
                    idSelector = "tm";
                } else {
                    document.getElementById('infoDiv').style.background = "#ff3a3a";
                    document.getElementById("resultInfo").innerHTML = myobj.message;
                    $("#tm").val("");
                    $("#tm").select();
                    idSelector = "tm";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
                getCheckTray(trayyinfoidnew, true);
            } else {
                $("#tm").val("");
                $("#tm").select();
                idSelector = "tm";
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //扫描条码
    function doScan(flag) {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            var sm = ret;
            //托码
            if (idSelector == 'trayyinfoidnew') {
                var text = document.getElementById("trayyinfoidnew");
                text.value = sm;
                // $("#tm").focus();
                getCheckTray(sm, false);
            } else if (idSelector == 'tm') {
                $("#tm").val(sm);
                //条码解析
                // tmSplit(sm);
                save();
            } else if (idSelector == 'workCellCode') {
                //查询来源库位
                getWorkCell(ret, idSelector, "1");
            }
        });
    }
    function getCheckTray(trayyinfoidnew, isreload) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getCheckTray";
        var data = {
            values: {
                trayyinfoid: trayyinfoidnew, //托码
                appVersion: api.appVersion,
                un: un
            }
        };
        api.showProgress({
            title: '加载中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            api.hideProgress();
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.list != undefined) {
                    var countQty = myobj.countQty;
                    var sumQty = myobj.sumQty;
                    $("#countQty").val(countQty);
                    $("#sumQty").val(sumQty);

                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<div class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 110px;padding: 0.2rem 0.2rem;font-size: 0.7rem;'>";
                        html += "<table style='width:100%;'>";
                        html += "<tr style='width:100%;'><td style='width:16%;'>箱号：</td><td  id='boxnumInfo' style='width:70%;' colspan='3'>" + list[i].boxnum + "</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:20%;'>物料号：</td> <td  id='mrlCodeInfo' style='width:70%;' colspan='3'>" + list[i].mrlCode + "</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:20%;'>批次号：</td><td id='lotCodeInfo' style='width:40%;' >" + list[i].lotCode + "</td> ";
                        html += "<td style='width:16%;'>数量：</td><td id='qtyInfo' style='width:20%;'>" + list[i].qty + "</td>";
                        html += "</tr>";
                        html += "<tr style='width:100%;'>";
                        html += "<td style='width:20%;'>仓库码：</td><td id='lotCodeInfo' style='width:30%;' >" + (list[i].workCenterCode == null ? "" : list[i].workCenterCode) + "</td>";
                        html += "<td style='width:20%;'>库位码：</td><td id='qtyInfo' style='width:30%;'>" + (list[i].workCellCode == null ? "" : list[i].workCellCode) + "</td></tr>";
                        html += "</table></div>";

                    }
                    $api.html($api.byId("tbodyHtml"), html);
                }

                //成功
                if (myobj.code == '0') {
                    if (!isreload) {
                        //扫描箱后重新加载托信息，返回信息不覆盖
                        document.getElementById('infoDiv').style.background = "#66f10b";
                        document.getElementById("resultInfo").innerHTML = "托信息加载成功";
                    }

                    if (myobj.cellCode != '') {
                        //若新托下箱库位一致，则使用新托的库位，设置库位不可选
                        workCellGid = myobj.cellId; //库位GID
                        workCellCode = myobj.cellCode; //库位编码
                        workCellName = myobj.cellName; //库位名称
                        $("#workCellCode").val(workCellCode + ":" + workCellName);
                        $("#workCellCode").attr('disabled', true);
                        //设置库位选择点击事件无效
                        document.getElementById("cellSelect").onclick = function () { };
                        $("#tm").select();
                        idSelector = "tm";
                    } else {

                        if (isreload) {
                            //扫描箱后重新加载托信息，则光标应还在箱号处
                            $("#tm").focus();
                            $("#tm").select();
                            idSelector = "tm";
                        } else {
                            $("#tm").focus();
                            $("#tm").select();
                            idSelector = "tm";
                            // workCellGid = ""; //库位GID
                            // workCellCode = ""; //库位编码
                            // workCellName = ""; //库位名称
                            // //新托下还没有箱信息，需要选择库位
                            // $("#workCellCode").val("");
                            // $("#workCellCode").attr('disabled', false);
                            // document.getElementById("cellSelect").setAttribute("onclick", "goSelectWorkCell('2')");
                            // $("#workCellCode").focus();
                            // $("#workCellCode").select();
                            // idSelector = "workCellCode";
                        }

                    }




                } else if (myobj.code == '1') {
                    document.getElementById('infoDiv').style.background = "#ff3a3a";
                    document.getElementById("resultInfo").innerHTML = myobj.message;
                    $("#trayyinfoidnew").val("");
                    $("#trayyinfoidnew").select();
                    idSelector = "trayyinfoidnew";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("连接超时，请耐心等待");
            }
        });
    }

    //新托码监听回车事件
    $("#trayyinfoidnew").on('keypress', function (e) {
        var keycode = e.keyCode;
        var trayyinfoidnew = $(this).val();
        if (keycode == '13') {
            if (trayyinfoidnew != null && trayyinfoidnew != "" && trayyinfoidnew != undefined) {
                $("#trayyinfoidnew").val(trayyinfoidnew);
                // $("#tm").focus();
                //判断托下是否有箱，若有箱，则继续判断
                // 1、是否入库，若未入库则不能转移 2、都入库，判断库位是否一致，不一致不能转移，一致则页面使用此库位且不能修改
                getCheckTray(trayyinfoidnew, false);
            }

        }
    });
    //条码监听回车事件
    $("#tm").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm)
                save();
                //条码解析
                // tmSplit(tm);
            }
        }
    });

    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        $("#supplierCode").val(arr[0]);
        $("#mrlCode").val(arr[1]);
        $("#qty").val(arr[2]);
        $("#lotCode").val(arr[3]);
        $("#tmID").val(arr[4]);
        $("#boxnum").val(arr[9]);
        //箱号选中
        $("#tm").select();
        //箱码转移
        save();
    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    //箱码转移
                    save();
                } else if (myobj.code == '1') {

                    $("#tm").val("");
                    $("#tm").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //7:库位扫描校验
    function getWorkCell(code, inputId, flag) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "out_kj", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: "", //仓库GID
                appVersion: api.appVersion,
                workCellCode: code, //库位编码
                relWorkCellGid: "", //目标库位
                tm: "",
                workCellFlag: "", //原库位目标库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);

                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#tm").focus();
                    idSelector = "tm";

                } else {
                    // api.toast({
                    //     msg: myobj.message,
                    //     duration: 3000
                    // });
                    document.getElementById('infoDiv').style.background = "#ff3a3a";
                    document.getElementById("boxnumInfo").innerHTML = "";
                    document.getElementById("mrlCodeInfo").innerHTML = "";
                    document.getElementById("lotCodeInfo").innerHTML = "";
                    document.getElementById("qtyInfo").innerHTML = "";
                    document.getElementById("resultInfo").innerHTML = myobj.message;

                    $("#" + inputId).focus();
                    $("#" + inputId).select();

                     api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //8：选择库位
    function goSelectWorkCell(workCellFlag) {
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "in_rk",
                name: "wmTrayinfo_Move",
                tm: "",
                workCenterGid: "",
                relWorkCellGid: "",
                workCellFlag: workCellFlag,
                centerClass: isSFK ? "SFK" : ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name, workCellFlag) {
        workCellGid = id;
        workCellCode = code;
        workCellName = name;
        document.getElementById('workCellCode').value = code + ":" + name;

        $("#tm").focus();
        var e = "tm";
        idSelector = e;
    }

    //9；PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#workCellCode").val(sm);
            getWorkCell(sm, "workCellCode", "1");
        }
    });


    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };
</script>

</html>